/*jslint browser: true, devel: true, bitwise: true, white: true, plusplus: true, forin:true, nomen:true */
/*global $:true, define:true */

/**
 * Includes Components for GUI, like buttons, window or text field.
 * @module UI
 * @main Widget
 */

define(function() {
"use strict";

/** 
 * Base for all user controls.
 * @class Widget
 * @constructor
 * @param {Object} props Parameters for this.
 *	@param {String} [props.id] ID for DOM native element.
 *  @param {Object} [props.style] Styles for DOM native element. 
 *   Keys are style property name and values are values for this properties.
 *  @param {Object} [props.events] Handlers for events. 
 *   Property names are style property name and values are values for this.
 *	@param {Boolean} [props.disabled] If true, widget is disabled on begin.
 *	@param {Boolean} [props.hidden] If true, widget is hidded on begin.
 *  @param {Container} props.parent Reference to container widget for this.
 */
function Widget(props, nativeType) {
	this.ntv = document.createElement(nativeType);
	this.Node = undefined; 
	this.Parent = undefined; 
	
	this.ntv.id = props.id||'';
		
	this.style = props.style;
	this.disabled = props.disabled;
	this.visibled = props.hidden||true;
	this.parent = props.parent;
		
	if (props.events) {
		this.setEvents(props.events);	
	}
}

Widget.prototype = {
	constructor: Widget,
	
	on: function(eventType, callback) {
		$(this.ntv).on(eventType,callback);
	},
	
	setEvents: function(events) {
		$(this.ntv).on(events);
	},
	
	toString: function() {
		return this.type()+' '+this.id;
	},
	
	type: function() {
		return 'Widget'; 
	}
};

Widget.defineProps = function(widgetClass,props) {
	Object.defineProperties(widgetClass,props);
};

Widget.defineProps(Widget.prototype,{
	
	id: {
		get: function() {
			return this.ntv.id;
		}	
	},
	
	style: {
		get: function() {
			return this.ntv.style;
		},
		
		set: function(props) {
				var s, ns = this.ntv.style;
				for (s in props) {
					ns[s] = props[s];
				}
		}
	},
	
	disabled: {
		get: function() {
			return this.ntv.disabled;
		},
		set: function(disable) {
			this.ntv.disabled = Boolean(disable);
		}
	},
	
	visibled: {
		get: function() {
			return Boolean(this.ntv.style.display);
		},
		set: function(visible) {
			this.ntv.style.display = (visible)?'':'none';
		}
	},
	
	parent: {
		get: function() {
			return this.Parent;
		},
		set: function(parent) {
			if (this.Parent) {
				this.Parent.removeChild(this);
			}
		
			if (parent) {
				if (!parent.addChild) { // czy Parent jest Pojemnikiem na inne kontrolki.
					throw new Error('[Widget] parent: '+parent.toString()+' is not Container');
				} else {
					parent.addChild(this);
				}
			}
		}
	}
});

return Widget;
});
